package com.maxsalad.uc.service;

import com.maxsalad.common.message.MessageResult;
import com.maxsalad.uc.dao.PlatformDao;
import com.maxsalad.uc.entity.ComboBoxItem;
import com.maxsalad.uc.pojo.TPlatformEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016-3-13.
 */
@Service
public class PlatformService extends BaseService {
    private static final Logger log = LoggerFactory.getLogger(PlatformService.class);

    @Autowired
    private PlatformDao platformDao;

    public Map queryAllPlatform(TPlatformEntity platformEntity) throws Exception {
        return platformDao.queryAllPlatform(platformEntity);
    }

    public List<TPlatformEntity> queryAllPlatformNoPage(TPlatformEntity platformEntity) throws Exception {
        return platformDao.queryAllPlatformNoPage(platformEntity);
    }

    /**
     * 获取平台下拉框数据
     *
     * @param platformEntity
     * @param index          默认选中的位置,从1开始
     * @return
     * @throws Exception
     */
    public List<ComboBoxItem> queryAllPlatformDropDown(TPlatformEntity platformEntity, int... index) throws Exception {
        List<TPlatformEntity> list = platformDao.queryAllPlatformNoPage(platformEntity);
        List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
        ComboBoxItem item;
        for (TPlatformEntity entity : list) {
            item = new ComboBoxItem();
            item.setId(String.valueOf(entity.getId()));
            item.setText(entity.getPlatformName());
            item.setDesc(entity.getPlatformCode());
            items.add(item);

            if (index != null && index.length > 0) {
                if (items.size() == index[0]) {
                    item.setSelected(true);
                } else {
                    item.setSelected(false);
                }
            }
        }

        return items;
    }

    public MessageResult saveOrUpdatePlatform(TPlatformEntity platformEntity) throws Exception {
        if (StringUtils.isBlank(platformEntity.getPlatformName())) {
            return getMessageResult("E00002");
        }
        if (StringUtils.isBlank(platformEntity.getPlatformName())) {
            return getMessageResult("E00003");
        }
        platformDao.saveOrUpdate(platformEntity);
        return getMessageResult("S00001");
    }

    public MessageResult deletePlatform(long id) {
        platformDao.delete(id);
        return getMessageResult("S00001");
    }
}
